/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.sd.jxta.task;

import co.edu.sd.jxta.msn.MessageMSPFactory;
import co.edu.sd.jxta.util.DateUtil;
import co.edu.sd.jxta.util.FileUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author SD
 */
public class IOTask {

    private static BASE64Decoder decoder;
    private static BASE64Encoder encoder;
    private static IOTask instance;


    static {
        decoder = new BASE64Decoder();
        encoder = new BASE64Encoder();
    }

    public static IOTask getInstance() {
        if (instance == null) {
            instance = new IOTask();
        }
        return instance;
    }

    public boolean write(String sRootFolder, Message msg) {
        File oRootFolder = new File("MySharedFiles/" + sRootFolder);
        MessageElement content = msg.getMessageElement(null, "" + MessageMSPFactory.Attributes.TAG_FILE_CONTENT);
        MessageElement name = msg.getMessageElement(null, "" + MessageMSPFactory.Attributes.TAG_FILE_NAME);

        try {
            System.out.println("writing content content to: " + oRootFolder + "/" + name);
            FileUtil.writeBytesIntoFile(oRootFolder + "/" + name, decoder.decodeBuffer(content.toString()));
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean writeFile(String sRootFolder, List <Message>msgs, String sPostfix) {
        File oRootFolder = new File("MySharedFiles/" + sRootFolder);
        //MessageElement content = msg.getMessageElement(null, "" + MessageMSPFactory.Attributes.TAG_FILE_CONTENT);

        MessageElement name = msgs.get(0).getMessageElement(null, "" + MessageMSPFactory.Attributes.TAG_FILE_NAME);
        String currentContent = "";
        try {
            currentContent = FileUtil.getBase64EncodeFromFile(oRootFolder + "/" + name + sPostfix);
        } catch (IOException ex) {
            //this exception is expected
        }
        String newContent = "";
        for (int i = 0; i < msgs.size(); i++) {
            Message msg = (Message)msgs.get(i);
            if (msg != null) {
                newContent += msg.getMessageElement(null, "" + MessageMSPFactory.Attributes.TAG_FILE_CONTENT);
            }

        }
        String content = currentContent + newContent;
        try {
            System.out.println("writing content to: " + oRootFolder + "/" + name);
            FileUtil.writeBytesIntoFile(oRootFolder + "/" + name + sPostfix, decoder.decodeBuffer(content));
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean find(String sRootFolder, String sFileRequested) {
        File oRootFolder = new File("MySharedFiles/" + sRootFolder);
        System.out.println("searching on directory: " + oRootFolder + " - for file - " + sFileRequested);
        String[] oFiles = oRootFolder.list();
        for (int i = 0; i < oFiles.length; i++) {
            if (oFiles[i].indexOf(sFileRequested) > -1) {
                System.out.println("-----------------------------------file found-----------------------------------");
                return true;
            }

        }
        return false;
    }

    public String execute(String command) {
        String output = "";
        try {
            Process oProcess;
            if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1) {
                oProcess = Runtime.getRuntime().exec("cmd /c " + command);
            } else {
                oProcess = Runtime.getRuntime().exec("bash -c " + command);
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(oProcess.getInputStream()));
            StringBuffer sReturn = new StringBuffer();
            String aux = br.readLine();
            while (aux != null) {
                sReturn.append(aux + "\n");
                aux = br.readLine();
            }
            output = sReturn.toString();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        System.out.println(output);
        return output;
    }

    public String time() {
        return "" + DateUtil.getCurrentTimeUTC();
    }
}
